﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace game
{
    public class SoundManager
    {
        protected SoundEffect plasmaSound;
        protected SoundEffect blasterSound;
        protected SoundEffect hitSound;
        protected SoundEffect shipSound;
        protected SoundEffect asteroidSound;

        Scene scene;

        public SoundManager(Scene scene)
        {
            this.scene = scene;

            // SoundEffect.MasterVolume = 1.0f;

            plasmaSound = ShooterGame.Instance.Content.Load<SoundEffect>("Sounds\\plasma");
            blasterSound = ShooterGame.Instance.Content.Load<SoundEffect>("Sounds\\laser");
            hitSound = ShooterGame.Instance.Content.Load<SoundEffect>("Sounds\\boom1");
            shipSound = ShooterGame.Instance.Content.Load<SoundEffect>("Sounds\\explosin");
            asteroidSound = ShooterGame.Instance.Content.Load<SoundEffect>("Sounds\\explosion_feu");

            Song song = ShooterGame.Instance.Content.Load<Song>("Sounds\\10-hoth-action");
            MediaPlayer.Volume = 0.1f;
            MediaPlayer.Play(song);
        }

        const float MAX_HEAR_DIST = 300000.0f;
        const float MAX_HEAR_DIST_INV = 1.0f / 3000000.0f;

        protected float getVolume(Vector3 position)
        {
            float dist = Vector3.Subtract(position, scene.PlayerObject.Position).Length();

            if (dist > MAX_HEAR_DIST)
                return 0.0f;
            else
                return (MAX_HEAR_DIST - dist) * MAX_HEAR_DIST_INV;
        }

        public void Plasma(Vector3 position)
        { 
            plasmaSound.Play(getVolume(position) * 0.2f, 0.0f, 0.0f);
        }

        public void Blaster(Vector3 position)
        {
            blasterSound.Play(getVolume(position) * 0.3f, 0.0f, 0.0f);
        }

        public void Hit(Vector3 position)
        {
            hitSound.Play(getVolume(position) * 0.7f, 0.0f, 0.0f);
        }

        public void Ship(Vector3 position)
        {
            shipSound.Play(getVolume(position), 0.0f, 0.0f);
        }

        public void Asteroid(Vector3 position)
        {
            asteroidSound.Play(getVolume(position), 0.0f, 0.0f);
        }


        public void Update(GameTime gameTime)
        {
            //blasterSoundElapsedTimeMs += gameTime.ElapsedGameTime.Milliseconds;
        }
    }
}
